// FILEPATH: e:/Work/CrazyFood/Assets/Script/Base/Manager/PropsManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PropsManager : MonoBehaviour
{
    public static PropsManager Instance { get; private set; }
    // 定义三种道具类型
    public enum PropType
    {
        Prop1,
        Prop2,
        Prop3,
        Prop4,
    }

    // 使用字典来存储道具类型和对应的数量
    private Dictionary<PropType, int> propCounts = new Dictionary<PropType, int>();

    // 在 Awake 方法中加载道具数量
    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        LoadPropCounts();
    }

    // 初始化道具数量
    private void InitializePropCounts()
    {
        foreach (PropType propType in System.Enum.GetValues(typeof(PropType)))
        {
            propCounts[propType] = 0;
        }
    }

    // 从本地加载道具数量
    private void LoadPropCounts()
    {
        foreach (PropType propType in System.Enum.GetValues(typeof(PropType)))
        {
            int count = PlayerPrefs.GetInt(propType.ToString(), 1);
            propCounts[propType] = count;
        }
    }

    // 保存道具数量到本地
    private void SavePropCounts()
    {
        foreach (var prop in propCounts)
        {
            PlayerPrefs.SetInt(prop.Key.ToString(), prop.Value);
        }
        GlobalEventManager.EmitEvent("UpdatePropsCount");
        PlayerPrefs.Save();
    }

    // 获取指定道具的数量
    public int GetPropCount(PropType propType)
    {
        return propCounts[propType];
    }

    // 增加指定道具的数量
    public void AddProp(PropType propType, int amount = 1)
    {
        propCounts[propType] += amount;
        SavePropCounts();
    }

    // 减少指定道具的数量
    public bool RemoveProp(PropType propType, int amount = 1)
    {
        if (propCounts[propType] >= amount)
        {
            propCounts[propType] -= amount;
            SavePropCounts();
            AudioManager.Instance.PlaySoundEffect("sounds_used");
            return true;
        }
        return false;
    }

    // 重置所有道具数量
    public void ResetAllPropCounts()
    {
        InitializePropCounts();
        SavePropCounts();
    }
}